;
(function() {
	new Vue({
		el: '#projectGl',
		data: {
			isExtenseFlag: false,
			keyWord: '',
			pageNums: [{name: '10', code: '10'}, {name: '15', code: '15'}, {name: '20', code: '20'}],
			page: 1,
			limit: ['10'],
			tbTitleWidth: {},
			listMenu: [{
					name: '未跟进过',
					num: '0',
					code: 'FMWF'
				},
				{
					name: '跟进中',
					num: '0',
					code: 'FMFI'
				},
				{
					name: '逾期未跟进',
					num: '0',
					code: 'FMOWF'
				},
				{
					name: '已关闭',
					num: '0',
					code: 'FMGB'
				},
				{
					name: '转运营',
					num: '0',
					code: 'FMYY'
				}
			],
			listMenuArrActive: [],
			listObj: {},
			rperson: {
				ids: [],
				name: null,
				list: []
			},
			provinceId: [],
			cityId: [],
			areaId: [],
			channelCategoryCode: [],
			channelNameCode: [],
			columns: '#',
			orders: '#',
			provinceList: [],
			cityList: [],
			areaList: [],
			keyword: '',
			channelCateArr: [],
			channelNameArr: [],
			beginDate: '',
			endDate: '',

			//自定义开始
			configType: 'WZ', //默认完整WZ     JD简单，WZ完整，自定义配置PZ
			module: '#',
			headArr: [],
			sortTableTitle: [],
			isScroll: false,
			isShowTableLayer: false,
			dateTimeUpdate: [],
			sortList: [], //新增排序数组
			isActiveCode: '#',
			leftWidth: 0,
			rightWidth: 0,
			leftTableArr: [],
			rightTableArr: [],
			tbTitleWidth: {},
			lefthover: 0,
			tophover: 0,
			texthover: false,
			textLong: '',
		},
		filters: {
			date(time) {
				if (!time) return '--'
				return hlDateUtil.formatUTCDate(time, 'yyyy-MM-dd HH:mm')
			},
			formatUndefined: function(item) {
				if (!item) return '--'
				if (typeof item == 'number') {
					if (!isNaN(item) && item.toString().length == 13) {
						return hlDateUtil.formatUTCDate(item, 'yyyy-MM-dd HH:mm')
					};
				}
				return item
			},
		},
		methods: {
			//显示hover
			hoverText(e, v) {
				var s = this.$refs.scrollLeft.scrollLeft;
				var l = e.srcElement.parentNode.offsetLeft - s;
				var t = e.srcElement.parentNode.offsetTop + 40
				console.log(l, t)
				this.lefthover = l + 'px';
				this.tophover = t + 'px';
				this.texthover = true;
				this.textLong = v;
			},
			mouseleaveOut() {
				this.lefthover = 0 + 'px';
				this.tophover = 0 + 'px';
				this.texthover = false;
				this.textLong = '';
			},

			//项目管理tab
			resetTab() {
				this.page = 1;
				this.listMenuArrActive = [];
				this.listDt()
			},
			//去详情
			goTodetail(id) {
				hlUrlUtil.goTo('projectDetail', {
					'id': id,
					ddtab: true
				}, true)
			},
			selectStatus(item) {
				this.page = 1;
				setTimeout(() => {
					this.listDt()
				}, 300)
				if (this.listMenuArrActive.indexOf(item.code) == -1) {
					this.listMenuArrActive.push(item.code)
					return
				}
				this.listMenuArrActive.splice(this.listMenuArrActive.indexOf(item.code), 1)
			},
			//获取渠道分类
			channelCateDt() {
				hlhttp.get({
					url: 'configure/dict',
					data: {
						module_code: 'SCTZ',
						config_name: 'SCTZ_CHANNEL_CATE',
						parent_dict_code: '#'
					},
					success: (data) => {
						this.channelCateArr = hlArrayUtil.replaceData(data, [{key: 'dictName', rKey: 'name'}, {key: 'dictCode', rKey: 'code'}]);
					}
				})
			},
			//获取渠道名称
			channelNameDt() {
				hlhttp.get({
					url: 'configure/dict',
					data: {
						module_code: 'SCTZ',
						config_name: 'SCTZ_CHANNEL_NAME',
						parent_dict_code: this.channelCategoryCode
					},
					success: (data) => {
						this.channelNameArr = hlArrayUtil.replaceData(data, [{key: 'dictName', rKey: 'name'}, {key: 'dictCode', rKey: 'code'}]);
					}
				})
			},
			//选择渠道分类
			selectedCC (item) {
				this.channelCategoryCode = [item[0].dictCode];
				this.channelNameArr = [];
				this.channelNameDt()
			},
			//选择渠道名称
			selectedCN (item) {
				this.channelNameCode = [item[0].dictCode];
			},
			//输入对接人进行搜索
			onInputDJR: function(e) {
				if (!this.rperson.name) {
					if (!e.data) {
						this.rperson.name = '';
						this.getDataGroup()
						return
					};
					this.rperson.name = e.data;
				}
				this.getDataGroup()
			},
			//判断有没有ids
			ifrpersonIds: function() {
				if (!this.rperson.ids.length) {
					this.rperson.name = null
				}
			},
			//设置对接人name，获取id
			setRperson: function(name, id) {
				this.rperson.ids = [];
				this.rperson.name = name;
				this.rperson.ids.push(id)
				this.rperson.list = [];
			},
			//搜索负责人工作组组员列表
			getDataGroup: function() {
				this.rperson.ids = [];
				hlhttp.get({
					url: 'tenants/users/_search',
					data: {
						keyword: this.rperson.name || '#',
						first_order: 'Y'
					},
					success: (res) => {
						this.rperson.list = res;
						for (var i = 0; i < this.rperson.list.length; i++) {
							if (this.rperson.name.match(res[i])) {
								this.rperson.ids.push(res[i].id)
							}
						}
					}
				})
			},
			//关闭对接人
			blurRperson() {
				setTimeout(() => {
					this.rperson.list = []
				}, 100)
			},
			//展开收起
			openOffOn() {
				this.isExtenseFlag = !this.isExtenseFlag
			},
			//省
			provinceData() {
				hlhttp.get({
					url: 'sub_regions',
					data: {
						region_id: '1'
					},
					success: (data) => {
						this.provinceList = hlArrayUtil.replaceData(data, [{key: 'regionId'}]);
					}
				})
			},
			//设置省
			setProvince(data) {
				this.provinceId = [data[0].regionId];
				this.cityData()
				this.areaList = [];
			},
			//市
			cityData() {
				hlhttp.get({
					url: 'sub_regions',
					data: {
						region_id: this.provinceId[0]
					},
					success: (data) => {
						this.cityList = hlArrayUtil.replaceData(data, [{key: 'regionId'}]);
					}
				})
			},
			setCity(data) {
				this.cityId = [data[0].regionId];
				this.areaData()
			},
			//区
			areaData() {
				hlhttp.get({
					url: 'sub_regions',
					data: {
						region_id: this.cityId[0]
					},
					success: (data) => {
						this.areaList = hlArrayUtil.replaceData(data, [{key: 'regionId'}]);
					}
				})
			},
			setArea(data) {
				this.areaId = [data[0].regionId];
			},
			//市场拓展项目管理列表
			listDt() {
				hlhttp.get({
					url: 'market/expension_project/followup_manager_search',
					data: {
						"keyword": this.keyword || '#', // 信息搜索(项目名称proj_name、联系人contact_name、联系人电话contact_phone_num、创建人creator_name)
						"follow_user_id": this.rperson.ids.length ? this.rperson.ids.toString() : '#', // 对接人
						"province_id": this.provinceId.length ? this.provinceId[0] : '#',// 项目所在地：省份
						"city_id": this.cityId.length ? this.cityId[0] : '#',// 项目所在地：城市
						"district_id": this.areaId.length ? this.areaId[0] : '#',// 项目所在地：区县
						followup_time_begin_time: this.beginDate || '#',
						followup_time_end_time: this.endDate || '#',
						"channel_category_code": this.channelCategoryCode.length ? this.channelCategoryCode[0] : '#',// 渠道分类
						"channel_name_code": this.channelNameCode.length ? this.channelNameCode[0] : '#',// 渠道名称
						"followup_status": this.listMenuArrActive.length ? this.listMenuArrActive.toString() : '#', // 状态：意向,已关闭,转运营   逗号,连接
						"column": this.columns,
						"order": this.orders,

						"limit": this.limit[0], // 分页参数，一页显示数量
						"page": this.page // 分页参数，页码
					},
					success: (data) => {
						let index = 1;
						data.expansionProjs.map(item => {
							if (!item.followupRecords.length) {
								item.followupRecords = [{}]
							}
							item.followupRecords.map(item0 => {
								item0.index = index;
								index++;
							})
						})
						this.listObj = data;
						$("#page").initPage(data.totalPage, this.page, (n) => {
							if (this.page == n) {
								return
							}
							this.page = n;
							this.listDt();
						});
						this.listNumberDt()
					}
				})
			},
			//获取统计数量
			listNumberDt() {
				hlhttp.get({
					url: 'market/expension_project/followup_manager_statistics',
					data: {
						"keyword": this.keyword || '#', // 信息搜索(项目名称proj_name、联系人contact_name、联系人电话contact_phone_num、创建人creator_name)
						"follow_user_id": this.rperson.ids.length ? this.rperson.ids.toString() : '#', // 对接人
						"province_id": this.provinceId.length ? this.provinceId[0] : '#',// 项目所在地：省份
						"city_id": this.cityId.length ? this.cityId[0] : '#',// 项目所在地：城市
						"district_id": this.areaId.length ? this.areaId[0] : '#',// 项目所在地：区县
						followup_time_begin_time: this.beginDate || '#',
						followup_time_end_time: this.endDate || '#',
						"channel_category_code": this.channelCategoryCode.length ? this.channelCategoryCode[0] : '#',// 渠道分类
						"channel_name_code": this.channelNameCode.length ? this.channelNameCode[0] : '#',// 渠道名称
						"followup_status": this.listMenuArrActive.length ? this.listMenuArrActive.toString() : '#', // 状态：意向,已关闭,转运营   逗号,连接
					},
					success: (data) => {
						this.listMenu.map(item => {
							switch (item.code) {
								case 'FMWF':
									item.num = data.FMWFCount
									break;
								case 'FMFI':
									item.num = data.FMFICount
									break;
								case 'FMOWF':
									item.num = data.FMOWFCount
									break;
								case 'FMGB':
									item.num = data.FMGBCount
									break;
								case 'FMYY':
									item.num = data.FMYYCount
									break;
								default:
									item.num = 0
									break;
							}
						})
					}
				})
			},
			//新增
			add() {
				hlUrlUtil.goTo('marketAdd', {
					ddtab: true
				}, true)
			},
			//重置
			reset() {
				this.keyword = ''
				this.rperson.ids = [];
				this.rperson.name = [];
				this.channelNameArr = []
				this.provinceId = []
				this.cityId = []
				this.areaId = [];
				this.beginDate = '';
				this.endDate = '';
				this.channelCategoryCode = []
				this.channelNameCode = [];
				this.listMenuArrActive = []
				this.columns = '#'
				this.orders = '#'
				this.isActiveCode = '',
				this.limit = ['10'];
				this.page = 1;
				this.listDt()
			},
			//翻页数量
			turnPage(item) {
				this.page = 1;
				this.limit = [item[0].code];
				this.listDt()
			},

			//更新时间
			sortBySTTime(item) {
				if(this.dateTimeUpdate.indexOf(item.value) == -1) return
        this.isActiveCode = item.value;
        this.orders = this.orders == 'A' ? 'D' : 'A';
        this.columns = item.orderName
				this.listDt()
			},
			//获取自定义表头配置状态
			configTypeDt() {
				hlhttp.get({
					url: 'configure/customize_column_header/type',
					data: {
						module: 'SCTZ_XMGJ'
					},
					success: (data) => {
						this.configType = data.configType;
						this.customizeHeaderDt()
					}
				})
			},
			//获取自定义表头配置信息
			customizeHeaderDt() {
				hlhttp.get({
					url: 'configure/customize_column_header',
					data: {
						module: 'SCTZ_XMGJ'
					},
					success: (data) => {
						data.customConfigList.map(item => {
							item.seq = Number(item.seq)
							item.customSeq = Number(item.customSeq)
							item.isActive = 'N';
							if (item.orderFlag == 'Y') {
								this.dateTimeUpdate.push(item.value)
							}
						})
						if (this.configType == 'JD' || this.configType == 'WZ') {
							data.customConfigList = data.customConfigList.sort(hlArrayUtil.compareAsc('seq'))
						}
						if (this.configType == 'PZ') {
							data.customConfigList = data.customConfigList.sort(hlArrayUtil.compareAsc('customSeq'))
						}

						this.headArr = JSON.parse(JSON.stringify(data.customConfigList));
						this.sortTableTitle = JSON.parse(JSON.stringify(data.customConfigList));
						this.lrTable()

					}
				})
			},
			//左右表格table
			lrTable() {
				this.leftWidth = 0;
				this.rightWidth = 0;
				this.leftTableArr = [];
				this.rightTableArr = [];

				this.headArr.map(item => {
					if (this.configType == 'JD') {
						if (item.baseFlag == 'Y') {
							this.lrTableDistinguish(item)
						}
					}
					if (this.configType == 'WZ') {
						this.lrTableDistinguish(item)
					}
					if (this.configType == 'PZ') {
						if (item.customShowFlag == 'Y') {
							this.lrTableDistinguish(item)
						}
					}
				})
				this.tbTitleWidthCalculation()
			},
			//左右表格区分
			lrTableDistinguish(item) {
				switch (item.unionTablePart) {
					case '1':
						this.leftTableArr.push(item)
						break;
					case '2':
						this.rightTableArr.push(item)
						break;
					default:
						break;
				}
			},
			//宽度计算
			tbTitleWidthCalculation() {

				this.leftTableArr.map(item0 => {
					this.leftWidth += Number(item0.styleWidth)
				})
				this.rightTableArr.map(item0 => {
					this.rightWidth += Number(item0.styleWidth)
				})
				this.tbTitleWidth = {
					width: (this.leftWidth + this.rightWidth) + 'px'
				}
				if (this.leftWidth + this.rightWidth < 960) {
					this.leftWidth = '50%'
					this.rightWidth = '50%'
					this.tbTitleWidth = {
						width: '100%'
					}
				}

			},
			//弹窗显示
			showTableLayer() {
				this.isShowTableLayer = true;
			},
			//关闭自定义表头弹窗
			closeTableLayer() {
				this.isShowTableLayer = false;
			},
			//简单完整配置切换
			selectTableType(type) {
				this.configType = type;
				this.changeTableType(type)
				this.updateConfigType()
			},
			//更新自定义表头配置状态
			updateConfigType() {
				hlhttp.put({
					url: 'configure/customize_column_header/type',
					data: {
						module: 'SCTZ_XMGJ',
						config_type: this.configType
					},
					success: () => {
						this.configTypeDt()
					}
				})
			},
			//改变头部
			changeTableType(type) {
				if (type == 'JD' || type == 'WZ') {
					this.headArr = this.headArr.sort(hlArrayUtil.compareAsc('seq'))
				}
				if (type == 'PZ') {
					this.headArr = this.headArr.sort(hlArrayUtil.compareAsc('customSeq'))
				}
				this.tbTitleWidthCalculation()
			},
			//显示某个自定义标题
			selectedTitleItem(i) {
				this.sortTableTitle[i].customShowFlag = 'Y'
			},
			//关闭某个自定义标题
			cancelSelectedTitleItem(i) {
				this.sortTableTitle[i].customShowFlag = 'N'
			},
			showhighLightTitleItem(el, i) {
				this.sortTableTitle[i].isActive = 'Y'
			},
			removehighLightTitleItem(el, i) {
				this.sortTableTitle[i].isActive = 'N'
			},
			//鼠标经过显示scroll
			showScroll() {
				this.isScroll = true;
			},
			//鼠标离开关闭scroll
			hideScroll() {
				this.isScroll = false;
			},
			// 确定提交自定义表格
			confirmSubmitTable() {
				let dataArr = [];
				this.sortTableTitle.map((item, index) => {
					dataArr.push({
						"base_flag": item.baseFlag,
						"seq": item.seq,
						"custom_show_flag": item.customShowFlag,
						"custom_seq": index,
						"custom_display_name": item.customDisplayName ? item.customDisplayName : '#',
						"dict_code": item.dictCode,
						"config_type": item.configType,
						"name": item.name,
						"value": item.value,
						"union_table_part": item.unionTablePart,
						flex: item.flex,
						style_width: item.styleWidth,
						order_flag: item.orderFlag,
						order_name: item.orderName
					})
				})
				hlhttp.put({
					url: 'configure/customize_column_header',
					data: {
						module: 'SCTZ_XMGJ',
						custom_config: JSON.stringify(dataArr)
					},
					success: (data) => {
						hl.toast('操作成功')
						this.closeTableLayer()
						this.updateConfigType()
					}
				})
			},
			// 初始化拖动排序
			initSortable() {
				const self = this;
				const el = document.getElementById('sortableTable');
				Sortable.create(el, {
					draggable: 'ul',
					onEnd(evt) {
						const beginIndex = evt.oldIndex;
						const endIndex = evt.newIndex;
						const newArr = JSON.parse(JSON.stringify(self.sortTableTitle));
						if (self.sortTableTitle[beginIndex].unionTablePart == self.sortTableTitle[endIndex].unionTablePart) {
							newArr.splice(endIndex, 0, newArr.splice(beginIndex, 1)[0])
						} else {
							hl.toast('合并表格无法跨表头排序')
						}
						self.sortTableTitle = [];
						self.$nextTick(() => {
							self.sortTableTitle = newArr
						})
					}
				})
			},
		},
		mounted() {
			hlSelectUi()
			this.provinceData()
			this.listDt()
			this.channelCateDt()
			this.listNumberDt()
			this.initSortable()
			this.configTypeDt()
			var _this = this;
			document.body.onclick = function () {
        _this.rperson.list = [];
      }
		}
	})
}())
